/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * KosztyView.java
 *
 * Created on 2011-04-06, 00:51:06
 */
package View;

import Controller.ElementyBudynku.Budynek;
import Controller.ElementyBudynku.Pietro;
import Controller.ElementyBudynku.Urzadzenie;
import Controller.ElementyBudynku.Pokoj;
import Controller.Utils.Konwerter;
import Model.DAO.MsGazPradDAO;
import Model.DAO.MsPokojDAO;
import Model.DAO.MsUrzadzenieDAO;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;

/**
 *
 * @author beniu
 */
public class KosztyPanel extends javax.swing.JDialog {

    /** Creates new form KosztyView */
    public KosztyPanel(java.awt.Frame parent, boolean modal, List<Pokoj> pokoje) throws SQLException {
        super(parent, modal);
        roomListModel = new DefaultComboBoxModel();
        listModel = new DefaultListModel();
        covertListToComboBox(pokoje);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListaUrzadzen = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Koszty"); // NOI18N
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel1.setText("Koszty");
        jLabel1.setName("jLabelKoszty"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jListaUrzadzen.setModel(listModel);
        jListaUrzadzen.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListaUrzadzen.setName("jListaUrzadzen"); // NOI18N
        jListaUrzadzen.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jListaUrzadzenMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jListaUrzadzen);

        jLabel2.setText("Czas pracy:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Koszt:");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel4.setText("00:00:00");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 18));
        jLabel5.setText("0.00 zł");

        jComboBox1.setModel(roomListModel);
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addPopupMenuListener(new javax.swing.event.PopupMenuListener() {
            public void popupMenuCanceled(javax.swing.event.PopupMenuEvent evt) {
            }
            public void popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {
                jComboBox1PopupMenuWillBecomeInvisible(evt);
            }
            public void popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent evt) {
            }
        });

        jSeparator1.setName("jSeparator1"); // NOI18N

        jSeparator2.setName("jSeparator2"); // NOI18N

        jLabel6.setText("Pokój:");
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText("Urządzenia:");
        jLabel7.setName("jLabel7"); // NOI18N

        jButton1.setText("Zmień cenę prądu/gazu");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 505, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(73, 73, 73)
                                        .addComponent(jButton1))
                                    .addGroup(layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel2)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(jLabel3)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                                .addGap(69, 69, 69))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel6)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 505, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(201, 201, 201)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel7)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(42, 42, 42)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3))
                        .addGap(57, 57, 57)
                        .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-472)/2, (screenSize.height-358)/2, 472, 358);
    }// </editor-fold>//GEN-END:initComponents

    public static double roundToDecimals(double d, int c) {
        int temp = (int) ((d * Math.pow(10, c)));
        return (((double) temp) / Math.pow(10, c));
    }

    private void jListaUrzadzenMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListaUrzadzenMouseClicked
        if (jListaUrzadzen.getModel().getSize() == 0) {
            return;
        }
        int selectedID = Integer.parseInt(jListaUrzadzen.getSelectedValue().toString().split("#")[1]);
        double prad = (new MsGazPradDAO()).pobierzCenePradu();
        MsUrzadzenieDAO dao = new MsUrzadzenieDAO();
        int czas = dao.pobierzCzasPracy(selectedID);
        double moc = dao.pobierzMoc(selectedID);
        jLabel4.setText(Konwerter.czasSekNaGodz(czas));//wyswietlanie czasu
        double kwh = czas * moc / 3600;//TODO: dla kWh dzielenie przez 3,600,000
        jLabel5.setText(roundToDecimals(kwh * prad, 2) + " zł");//wyswietlanie mocy
    }//GEN-LAST:event_jListaUrzadzenMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        cenaPanel = new CenaPanel(this, true);
        cenaPanel.setVisible(true);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jComboBox1PopupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent evt) {//GEN-FIRST:event_jComboBox1PopupMenuWillBecomeInvisible
        if (jComboBox1.getSelectedItem().toString().equals("wybierz...")) {
            listModel.clear();
            return;
        }
        int selectedRoomID = Integer.parseInt(jComboBox1.getSelectedItem().toString().split("#")[1]);
        try {
            initList(selectedRoomID);
        } catch (SQLException ex) {
            Logger.getLogger(KosztyPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jComboBox1PopupMenuWillBecomeInvisible

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    Pietro pietro = new Pietro(1); //TODO
                    KosztyPanel dialog = new KosztyPanel(new javax.swing.JFrame(), true, pietro.wylistujPokoje());
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                        @Override
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(KosztyPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    private void initList(int idPokoju) throws SQLException {//wczytywanie nazw urzadzen z bazy danych do listy
        //ConnectionManager.getDatabaseConnection();
        listModel.clear();
        int n;
        list = (new MsPokojDAO()).wylistujUrzadzenia(idPokoju);
        n = list.size();
        String nazwaUrzadzenia = "";
        int idUrzadzenia = 0;
        for (int i = 0; i < n; i++) {
            idUrzadzenia = (Integer) list.get(i);
            Urzadzenie u = new Urzadzenie(idUrzadzenia);
            nazwaUrzadzenia = u.pobierzNazwa();
            if (nazwaUrzadzenia.equals("Okno") == false && nazwaUrzadzenia.equals("Drzwi") == false) {
                listModel.addElement(nazwaUrzadzenia + "#" + idUrzadzenia);
            }
        }

    }

    private void covertListToComboBox(List<Pokoj> pokoje) {
        roomListModel = new DefaultComboBoxModel();
        roomListModel.addElement("wybierz...");
        Iterator<Pokoj> it = pokoje.iterator();
        Pokoj p = new Pokoj();
        while (it.hasNext()) {
            p = it.next();
            roomListModel.addElement(p.getNazwa() + "#" + p.getIdPokoju());
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JList jListaUrzadzen;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    // End of variables declaration//GEN-END:variables
    private DefaultListModel listModel;
    private DefaultComboBoxModel roomListModel;
    private List list;
    private CenaPanel cenaPanel;
}
